package cn.wlh.learn.webmagic.utils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import us.codecraft.webmagic.selector.Selectable;
import us.codecraft.webmagic.selector.Selectors;
import us.codecraft.webmagic.selector.XpathSelector;

import java.util.List;

public abstract class SelectableUtils {
    private SelectableUtils(){};

    /**
     * 递归查找 Selectable
     * @param selectable
     * @param xpaths
     * @return
     */
    public static Selectable xpath(Selectable selectable,String... xpaths) {
        if ( ArrayUtil.isEmpty(xpaths)) return  null;
        Selectable ret = null;
        for (String xpath : xpaths) {
            ret = selectable.xpath(xpath);
            if(ret != null ) {
                // 继续看下面有不有内容.
//                List<Selectable> nodes = ret.nodes();
                if (StrUtil.isNotBlank(ret.get())) break;
            }
        }

        return ret;
    }
}
